#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
__author__ : xyh
__date__: 2016-09-14
A股市场公告爬虫(源:巨潮网),每日增量更新
"""

import os
import sys
from ConfigParser import ConfigParser
from datetime import datetime

from scpy.logger import get_logger
from xtls.timeparser import parse_time
from crawler.artificial_shenhu_stock_anno_cron import ShenhuCrawler
from queue.anno_producer import BuildShenhu
from queue.anno_producer import produce_message

reload(sys)
sys.setdefaultencoding("utf-8")
logger = get_logger(__file__)


def crawler():
    CATEGORY = {
        "category_ndbg_szsh": u"年度报告",
        "category_bndbg_szsh": u"半年度报告",
        "category_yjdbg_szsh": u"一季度报告",
        "category_sjdbg_szsh": u"三季度报告",
        "category_scgkfx_szsh": u"首次公开发行及上市",
        "category_pg_szsh": u"配股",
        "category_zf_szsh": u"增发",
        "category_kzhz_szsh": u"可转换债券",
        "category_qzxg_szsh": u"权证相关公告",
        "category_qtrz_szsh": u"其他融资",
        "category_qyfpxzcs_szsh": u"权益及限制出售股份",
        "category_gqbd_szsh": u"股权变动",
        "category_jy_szsh": u"交易",
        "category_gddh_szsh": u"股东大会",
        "category_cqfxyj_szsh": u"澄清风险业绩预告",
        "category_tbclts_szsh": u"特别处理和退市",
        "category_bcgz_szsh": u"补充及更正",
        "category_zjjg_szsh": u"中介机构报告",
        "category_ssgszd_szsh": u"上市公司制度",
        "category_zqgg_szsh": u"债券公告",
        "category_qtzdsx_szsh": u"其它重大事项",
        # "category_tzzgx_szsh": u"投资者关系信息",
        # addition 2016.7.8
        "category_dshgg_szsh": u"董事会公告",
        "category_jshgg_szsh": u"监事会公告",
    }

    for category in CATEGORY:
        logger.info('crawling %s-%s' % (category, CATEGORY[category]))
        ShenhuCrawler(category).run()


def publish_message(STARTUP_TIME):
    config = ConfigParser()
    config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'config.cfg'))
    with open(config_path, 'rb') as cfgfile:
        config.readfp(cfgfile)

    amqp_url = 'amqp://sc-admin:1qaz2wsx@10.51.29.242:5672/%2F?connection_attempts=3&heartbeat_interval=3600'
    queue_name = config.get('queue', 'daily_queue_name')
    start_anno_time = parse_time(u'1天前')
    stop_anno_time = parse_time(u'1秒前')
    build_message = BuildShenhu(start_anno_time=start_anno_time, stop_anno_time=stop_anno_time, STARTUP_TIME=STARTUP_TIME)

    produce_message(amqp_url, queue_name, build_message)


def main():
    STARTUP_TIME = datetime.today()  # 程序启动时间 eg: datetime.datetime(2016, 9, 14, 13, 53, 4)
    crawler()
    publish_message(STARTUP_TIME)


if __name__ == '__main__':
    main()